package cn.csxw.openchina.ui.fragment.syntheticalFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;
import cn.csxw.openchina.dagger2.component.DaggerBlogFragmentComponent;
import cn.csxw.openchina.dagger2.module.BlogFragmentModule;
import cn.csxw.openchina.dagger2.presenter.BlogFragmentPresenter;
import cn.csxw.openchina.ui.adapter.BlogAdapter;
import cn.csxw.openchina.utils.Global;

/**
 *  综合Fragment下的 博客Fragment
 */

public class BlogFragment extends BaseRefreshFragment {
    private int pageIndex = 0;
    @Inject
    public BlogFragmentPresenter mBlogFragmentPresenter;
    public BlogAdapter mBlogAdapter;

    @Override
    protected BaseRecAdapter getAdapter() {
        if(mBlogAdapter==null){
            mBlogAdapter = new BlogAdapter();
        }
        return mBlogAdapter;
    }

    @Override
    public void doRefresh() {
        mBlogAdapter.getData().clear();
        mBlogFragmentPresenter.callNetWork(0,20,"latest");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setOnRefreshListener(mOnRefreshListener);
        DaggerBlogFragmentComponent.builder().blogFragmentModule
                (new BlogFragmentModule(this)).build().inject(this);
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mBlogFragmentPresenter.callNetWork(0,20,"latest");
        Global.onNetworkStateListener(mBlogFragmentPresenter,this);
    }
    BaseRefreshFragment.OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        /**
         * 上拉加载更多
         */
        @Override
        public void onUpLoadMore() {
            if (Global.isNetworkReachable(getActivity())) {
                pageIndex++;
                mBlogFragmentPresenter.callNetWork(pageIndex,20,"latest");
            }else {
                Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
            }
        }
    };
}
